﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JibberJabber.LivePreviews;

namespace JibberJabber.PartDesigners
{
    public delegate void SpeakersListUpdated();

    public partial class SpeakersNodeDesigner : PartDesigner
    {
        public event SpeakersListUpdated SpeakersListUpdated;

        public SpeakersNodeDesigner()
        {
            InitializeComponent();
            lstSpeakers.Items.Clear();
        }

        public override Type[] AllowedChildren
        {
            get
            {
                return new Type[] { typeof(SpeakerDesigner) };
            }
        }

        public override string StartXml
        {
            get
            {
                return "<speakers>";
            }
        }

        public override string EndXml
        {
            get
            {
                return "</speakers>";
            }
        }

        public override void NotifyChildrenUpdated()
        {
            if (SpeakersListUpdated != null)
                SpeakersListUpdated();
            object selected = lstSpeakers.SelectedItem;
            lock (Children)
            {
                lstSpeakers.Items.Clear();
                foreach (PartDesigner p in Children)
                    lstSpeakers.Items.Add(p.ToString());
                if (selected != null && lstSpeakers.Items.Contains(selected))
                    lstSpeakers.SelectedItem = selected;
            }
            Program.NotifyNodeToStringOutputUpdated(this);
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            Program.AddChildPartDesigner(this, new SpeakerDesigner());
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (lstSpeakers.SelectedItem != null)
                lock (Children)
                    Children.Remove(Children[lstSpeakers.SelectedIndex]);
            NotifyChildrenUpdated();
            Program.NotifyNodesUpdated();
        }

        public override string ToString()
        {
            lock (Children)
            {
                if (Children.Count == 0)
                    return "Speakers List";
                else
                {
                    StringBuilder sb = new StringBuilder("Speakers (");
                    foreach (PartDesigner pd in Children)
                    {
                        sb.Append(pd.ToString());
                        sb.Append(", ");
                    }
                    sb.Remove(sb.Length - 2, 2);
                    sb.Append(")");
                    return sb.ToString();
                }
            }
        }

        private void lstSpeakers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSpeakers.SelectedIndex > -1)
            {
                lock (Program.Speakers.Children)
                    LivePreview.Preview(((SpeakerDesigner)Program.Speakers.Children[lstSpeakers.SelectedIndex]));
            }
        }
    }
}
